package com.ymatou.iapi.sellerproduct.testcase;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.BatchSetProductOffSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductsInLive;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

/**
*批量下架商品
*/
@TestSuite
public class Ts_BatchSetProductOffSale{
	private static BatchSetProductOffSaleCall batchSetProductOffSaleCall;
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String activityStartId = EnvSetup.getData("productActivityStartId");
	private static String activityUser = EnvSetup.getData("admin");
	private static String activityPswd = EnvSetup.getData("password");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量下架商品");
	}

	@Before
	public void caseUp() {
		batchSetProductOffSaleCall=new BatchSetProductOffSaleCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchSetProductOffSaleCall.getOpurl());
	}
	


	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_001() {
		Logger.start(true,"下架多个商品");
		try {
			//TODO 补全case代码

			//获取当前时间&结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
//			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID1, "响应报文-商品ID");
			
			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("UserID", sellerId);
			updateMap.put("LoginID", sellerName);
			String productID2 = SellerProductCallService.addFBXProduct(updateMap);
			Logger.verifyNotNull(productID2, "响应报文-商品ID");
			
			//Call batchSetProductOnSale
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			productidList.add(productID2);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID1);
			         
			//断言
			batchSetProductOffSaleCall.setOffSalecheck(startTime, productidList);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID2);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_002() {
		Logger.start(true,"下架单个商品（直播中商品）");
		try {
			//TODO 补全case代码

			//获取当前时间&结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			
			//创建一个正在进行的直播
			int activityId =LiveCallService.createLiveInProgress(sellerName , sellerId).getJSONObject("Data").getInt("ActivityId");
			Logger.verifyNotNull(activityId, "响应报文-直播ID");
			
			//发布一个立即上架商品
			String productID = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//将商品添加到直播
			SellerProductCallService.addProductInLive(sellerId, activityId, productID);
						
			List<String> productidList=new ArrayList<>();
			productidList.add(productID);
			
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);
			
			YMTDateUtil.waitTime(1);
			
			//Call 下架接口
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID);
			
			//断言
			batchSetProductOffSaleCall.setOffSalecheck(startTime, productidList);
			batchSetProductOffSaleCall.setOffSaleforliveProduct(productidList);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
			
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productID),"Mongo-LiveProducts 下架，直播商品记录删除");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityId,false);
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_003() {
		Logger.start(true,"下架直播商品之后再次加入该直播");
		try {
			//TODO 补全case代码
			
			//创建一个正在进中的直播
			Integer activityid = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个定时上架的商品并且加入进行中的直播
			List list = new ArrayList<>();
			list.add(activityid);
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("Product->LiveIdList", new JSONArray(list));
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, updatemap).getJSONObject("Data").getString("ProductID");
				
			//组装下架报文
			List<String> productidList=new ArrayList<>();
			productidList.add(productid);
			
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);
			
			//Call 下架接口
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productid);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 下架，直播商品记录删除");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityid,false);
				
			}
			
			
			//再次加入直播
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,activityid,productid);
			
			//check SQL
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(activityid).get(0);
			
			Map<String, Object> sqlTgtMap = new HashMap<String, Object>();
			sqlTgtMap.put("iuserid", sellerId); 
			sqlTgtMap.put("sproductid", productid); 
			sqlTgtMap.put("iactivityid", activityid); 
			sqlTgtMap.put("iAction", 0); 
			
			AssertService.assertResultEqual(sqlTgtMap, MapUtil.pojoToMap(ymtProductsInLive));	    	
			
			//check Mongo
			//Logger.verifyNotNull(new LiveProductModelWapper().getByProductId(productid), "重新加入直播后，Mongo商品直播表应不为空");
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				
				Logger.verifyEquals(1,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 上架，直播商品记录添加");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityid);
				
			}	
								
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_004() {
		Logger.start(true,"下架单个商品（FBX商品）");
		try {
			//TODO 补全case代码

			//获取当前时间&结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			
			//发布一个立即上架商品
			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("UserID", sellerId);
			updateMap.put("LoginID", sellerName);
			String productID = SellerProductCallService.addFBXProduct(updateMap);
			Logger.verifyNotNull(productID, "响应报文-商品ID");
						
			//商品id加入下架接口报文
			List<String> productidList=new ArrayList<>();
			productidList.add(productID);
			
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);
			
			//Call 下架接口
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID);
			
			//断言
			batchSetProductOffSaleCall.setOffSalecheck(startTime, productidList);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}	
							
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_005() {
		Logger.start(true,"下架单个商品（现货商品）");
		try {
			//TODO 补全case代码

			//获取当前时间&结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
//			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID1, "响应报文-商品ID");
			
			//Call batchSetProductOnSale
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID1);
			         
			//断言
			batchSetProductOffSaleCall.setOffSalecheck(startTime, productidList);
				
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_006() {
		Logger.start(false,"已下架商品再次下架");
		try {
			//TODO 补全case代码

			//获取当前时间&结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
//			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID1, "响应报文-商品ID");
			
			//Call batchSetProductOnSale
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			
			//再次call
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID1);
			         
			//断言
			batchSetProductOffSaleCall.setOffSalecheck(startTime, productidList);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_007() {
		Logger.start(false,"下架活动商品");
		try {
			//TODO 补全case代码

			//获取当前时间&结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个活动商品			
			String productID1 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");			
			Logger.verifyNotNull(productID1, "响应报文-商品ID");
			
			//报名审核通过活动
			SellerProductCallService.approveActivity(sellerId, activityUser , activityPswd , activityStartId , productID1);			
			
			YMTDateUtil.waitTime(1);
			
			//Call batchSetProductOnSale
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID1);
			         
			//断言
			//SQL YmtProducts
			List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productID1);
			String svalidStartbysql = (new SimpleDateFormat("yyyy-MM-dd")).format(productIDsqllist.get(0).get("validStart"));			
			String svalidEndbysql = (new SimpleDateFormat("yyyy-MM-dd")).format(productIDsqllist.get(0).get("validEnd"));						
			Logger.verifyEquals(startTime, svalidStartbysql, "商品开始销售时间");
			Logger.verifyEquals(endTime, svalidEndbysql, "商品结束销售时间");	
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//newMango ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 100);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productDetailModel.get("ValidEnd"), 100);
												
				//newMango ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 100);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productListingModel.get("ValidEnd"), 100);	
			}
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}		
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_008() {
		Logger.start(false,"下架商品id为空");
		try {
			//TODO 补全case代码

			//Call batchSetProductOnSale
			List<String> productidList=new ArrayList<>();
			productidList.add(null);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			
			JSONObject dataStr = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			
			//错误断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(201, code, "\"Code\":201");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(201, bCode, "\"BCode\":201");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("下架商品列表中有不属于当前用户的商品", msg, "下架商品列表中有不属于当前用户的商品");
			String trace =  dataStr.getString("Trace");
			Logger.verifyIsNull(trace, "Trace等于空");	
			String data =  dataStr.getString("Data");
			Logger.verifyIsNull(data, "Data等于空");	

						
		} catch (Exception e) {
			Logger.fail(e);
		}
		
		
		
		
	}	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_009() {
		Logger.start(false,"下架商品从多个直播");
		try {
			//创建多个直播
			List<Integer> lives = LiveCallService.createLivesInProcessAndInFuture(sellerId, sellerName, 3, 2);
			
			//发布一个立即上架商品
			String productID = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//将商品添加到直播
			lives.forEach(live->{
				try {
					SellerProductCallService.addProductInLive(sellerId, live, productID);
				} catch (JSONException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
			

			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				YMTDateUtil.waitTime(5);
				Logger.verifyEquals(lives.size(), new LiveProductsWapper().getCountByProductId(productID), "Mongo-liveproducts 个数检查");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
			
			//Call batchSetProductOnSale
			List<String> productidList=new ArrayList<>();
			productidList.add(productID);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			JSONObject result = batchSetProductOffSaleCall.callAndGetReturnData(requestMap);
			//断言返回报文
			batchSetProductOffSaleCall.batchSetProductOffSalereturncode(result, productID);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(0, new LiveProductsWapper().getCountByProductId(productID), "Mongo-liveproducts 个数检查");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
				lives.forEach(live->{
					try {
						NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(live,false);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				});
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
		
		
		
		
	}	
	
	
	
	
	

		



	
}